package ConnectionsCommands;

import Gear.Factory;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implement received command to register a list of files.
 *
 * @author Rodrigo Martins Racanicci
 */
public class ConnectionCommandRegisterFile implements ConnectionCommand {

    @Override
    public void execute(Socket client, String[] args) {
        if (args.length < 1) {
            return;
            //throw new IllegalArgumentException("Too few arguments to RegisterFile.");
        }

        try {
            Factory.getInstance().getTracker().getFileMutex().acquire();

            for (int i = 1; i < args.length; i++) {
                System.out.println(client.getInetAddress().toString() + " - " + args[i]);
                Factory.getInstance().getTracker().addFile(client.getInetAddress().toString(), args[i]);
            }

            Factory.getInstance().getTracker().getFileMutex().release();
        } catch (InterruptedException ex) {
            Logger.getLogger(ConnectionCommandRegisterFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
